﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace ProjectTask
{
    class ClassTask
    {
        private SqlConnection scKengee_IT_MIS;
        public DataTable dtTask= new DataTable();
        public ComboBox cbTaskState = new ComboBox();
        public ComboBox cbSolutionMode = new ComboBox();

        public ClassTask(SqlConnection scn)
        {
            scKengee_IT_MIS = scn;
            GetTaskItems();
        }

        public void TaskNewInitial()
        {            
            string sql = @"select * from JobTask where 1=2";
            SqlDataAdapter sda = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sda.Fill(dtTask);
            scKengee_IT_MIS.Close();
            sda.Dispose();
            dtTask.Rows.Add();
        }

        private void GetTaskItems()
        {
            string sql = @"select * from ConstantValue where TableName='JobTask' order by FieldName,Weight";
            DataTable dtShopInfoItems = new DataTable();
            SqlDataAdapter sdaShopInfo = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sdaShopInfo.Fill(dtShopInfoItems);
            scKengee_IT_MIS.Close();
            sdaShopInfo.Dispose();

            for (int i = 0; i < dtShopInfoItems.Rows.Count; i++)
            {
                string sFieldName = dtShopInfoItems.Rows[i]["FieldName"].ToString();
                string sValue = dtShopInfoItems.Rows[i]["Value"].ToString();
                switch (sFieldName)
                {
                    case "cbTaskState": cbTaskState.Items.Add(sValue); break;
                }
            }
        }

        public bool GetTask_ID(string ID)
        {
            bool result = false;
            string sql = @"select * from JobTask where id=" + ID;
            SqlDataAdapter sdaTask = new SqlDataAdapter(sql, scKengee_IT_MIS);
            dtTask = new DataTable();
            scKengee_IT_MIS.Open();
            sdaTask.Fill(dtTask);
            scKengee_IT_MIS.Close();
            sdaTask.Dispose();

            if (dtTask.Rows.Count == 1)
            {
                result = true;
            }
            return result;
        }

        public int TaskAppend()
        {
            CollectFastInput();
            return ProjectCommon.ClassCommon.ClassAppend(scKengee_IT_MIS, dtTask, "JobTask");
        }

        public int TaskModify()
        {
            CollectFastInput();
            return ProjectCommon.ClassCommon.ClassModify(scKengee_IT_MIS, dtTask, "JobTask");
        }

        private void CollectFastInput()
        {
            string sTaskType = dtTask.Rows[0]["tbTaskType"].ToString();
            string sTaskDepict = dtTask.Rows[0]["tbTaskObject"].ToString();
            string sqlObject = @"select count(*) from FastInput where (TaskType='" + sTaskType + "')and(TaskObject='对象') and (TaskDepict='" + sTaskDepict + "')";
            SqlCommand scm = new SqlCommand(sqlObject, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            int iCount = int.Parse(scm.ExecuteScalar().ToString());
            scKengee_IT_MIS.Close();
            scm.Dispose();
            if (iCount < 1)
            {
                sqlObject = @"INSERT INTO [kengee_it_mis].[dbo].[FastInput]
                                   ([TaskType]
                                   ,[TaskObject]
                                   ,[TaskDepict])
                             VALUES
                                   ('"+sTaskType+@"'
                                   ,'对象'
                                   ,'"+sTaskDepict+"')";
                sqlObject = ProjectCommon.ClassCommon.ToDBC(sqlObject);
                scm = new SqlCommand(sqlObject, scKengee_IT_MIS);
                scKengee_IT_MIS.Open();
                scm.ExecuteNonQuery();
                scKengee_IT_MIS.Close();
                scm.Dispose();
            }

            sTaskDepict = dtTask.Rows[0]["tbTaskDepict"].ToString();
            sqlObject = @"select count(*) from FastInput where (TaskType='" + sTaskType + "')and(TaskObject='描述') and (TaskDepict='" + sTaskDepict + "')";
            scm = new SqlCommand(sqlObject, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            iCount = int.Parse(scm.ExecuteScalar().ToString());
            scKengee_IT_MIS.Close();
            scm.Dispose();
            if (iCount < 1)
            {
                sqlObject = @"INSERT INTO [kengee_it_mis].[dbo].[FastInput]
                                   ([TaskType]
                                   ,[TaskObject]
                                   ,[TaskDepict])
                             VALUES
                                   ('" + sTaskType + @"'
                                   ,'描述'
                                   ,'" + sTaskDepict + "')";
                sqlObject = ProjectCommon.ClassCommon.ToDBC(sqlObject);
                scm = new SqlCommand(sqlObject, scKengee_IT_MIS);
                scKengee_IT_MIS.Open();
                scm.ExecuteNonQuery();
                scKengee_IT_MIS.Close();
                scm.Dispose();
            }
        }
    }
}
